package tr.gen._3bit.java._2k1s.web.action;

import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;

import tr.gen._3bit.java._2k1s.dao.model.SerialInterface;
import tr.gen._3bit.java._2k1s.dao.service.SerialInterfaceService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validation;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;

@Validation
public class SerialInterfaceAction extends ActionSupport implements Preparable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 5980481609319907617L;
	private SerialInterfaceService service;
    private List<SerialInterface> serialInterfaces;
    private SerialInterface serialInterface;
    private Integer id;

    public SerialInterfaceAction(SerialInterfaceService service) {
        this.service = service;
    }

    @SkipValidation
    public String execute() {
        this.serialInterfaces = service.findAll();
        return Action.SUCCESS;
    }

    public String save() {
        this.service.save(serialInterface);
        this.serialInterface = new SerialInterface();
        return execute();
    }

    @SkipValidation
    public String remove() {
        service.remove(id);
        return execute();
    }

    public List<SerialInterface> getSerialInterfaces() {
        return serialInterfaces;
    }

    @IntRangeFieldValidator(message = "", key = "serialInterface.action.error.id.int", min = "0")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void prepare() throws Exception {
        if (id != null)
            serialInterface = service.find(id);
    }

    @VisitorFieldValidator(message="")
    public SerialInterface getSerialInterface() {
        return serialInterface;
    }

    public void setSerialInterface(SerialInterface serialInterface) {
        this.serialInterface = serialInterface;
    }
}
